package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.dao.CarDao;
import com.wsd.smartcarwasher.domain.Car;
import com.wsd.smartcarwasher.util.ReturnUtil;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class CarService {

    @Resource
    private CarDao carDao;

    public ModelMap addCar(Car car) {
        ModelMap modelMap = ReturnUtil.Success(ResultState.CAR_RECORD_ADD_SUCCESS.getMsg(), car);
        if (carDao.queryCarByUserIdAndPlateNumber(car.getUserId(), car.getPlateNumber()) != null) {
            modelMap = ReturnUtil.Error(ResultState.CAR_RECORD_IS_EXIST.getMsg());
        }
        int changeRows = carDao.addCar(car);
        if (changeRows == 0) {
            modelMap = ReturnUtil.Error(ResultState.CAR_RECORD_ADD_FAIL.getMsg());
        }
        return modelMap;
    }

    public List<Car> getCarsByUserId(String userId) {
        List<Car> cars = carDao.getCarsByUserId(userId);
        if (cars == null) {
            return Collections.emptyList();
        }
        return cars;
    }
    public ModelMap removeCar(String userId, String plateNumber) {
        ModelMap modelMap = ReturnUtil.Success(ResultState.CAR_RECORD_REMOVE_SUCCESS.getMsg());
        Car car = carDao.queryCarByUserIdAndPlateNumber(userId, plateNumber);
        if (car == null) {
            return ReturnUtil.Error(ResultState.CAR_RECORD_IS_NULL.getMsg());
        }
        int changeRows = carDao.removeCar(car.getId());
        if (changeRows == 0) {
            modelMap = ReturnUtil.Error(ResultState.CAR_RECORD_REMOVE_FAIL.getMsg());
        }
        return modelMap;
    }
}
